package com.example.ice.fittime.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.example.ice.fittime.R;
import com.example.ice.fittime.Social_Act_Activity;
import com.example.ice.fittime.SocialActTickerActivity;
import com.example.ice.fittime.entry.Banners;
import com.example.ice.fittime.entry.CommentUser;
import com.example.ice.fittime.entry.HotUser;
import com.example.ice.fittime.entry.PraiseUser;
import com.example.ice.fittime.entry.Sticker;
import com.example.ice.fittime.entry.UserActivity;
import com.example.ice.fittime.fragment.SocialFragment;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/10/12.
 */

public class SocialRecyclerAdapter extends RecyclerView.Adapter<SocialRecyclerAdapter.SocialVH> implements View.OnClickListener {
    private final LayoutInflater inflate;
    private List<Object> list;
    private Context context;
    private int[] rIds = {R.layout.social_recycler_item1, R.layout.socia_recycler_click, R.layout.social_recycler_item2, R.layout.social_sticker_recycler_item, R.layout.social_hotuser_recycler_item};
    ;
    private SocialFragment fragment;
    private Activity activity;
    private RecyclerView recyclerView;

    public SocialRecyclerAdapter(List<Object> list, Context context, SocialFragment fragment) {
        this.list = list;
        this.context = context;
        inflate = LayoutInflater.from(context);
        this.fragment = fragment;
    }

    public SocialRecyclerAdapter(List<Object> list, Context context, Activity activity) {
        this.list = list;
        this.context = context;
        inflate = LayoutInflater.from(context);

        this.activity = activity;
        this.activity = activity;

    }

    @Override
    public SocialVH onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = this.inflate.inflate(rIds[viewType], parent, false);
        SocialVH holder = new SocialVH(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(SocialVH holder, int position) {
        Object o = list.get(position);
        if (o != null) {
            if (o instanceof UserActivity) {
                UserActivity item = (UserActivity) o;
                String video_url = item.getVideo_url();
                if(video_url !=null){
                    holder.play.setVisibility(View.VISIBLE);
                    Uri uri = Uri.parse("http://is.snssdk.com/neihan/video/playback/?video_id=be3e8dc9ab68492c951b9e8d08cfca09&quality=480p&line=0&is_gif=0.mp4");
                    holder.videoView.setVideoURI(uri);
                } else {
                    holder.play.setVisibility(View.GONE);
                    holder.play.setVisibility(View.GONE);
                }
                holder.name.setText(item.getUserDetail().getName());
                String content = item.getContent();
                if (content == null) {
                    content = item.getUserDetail().getDescription();
                }
                if (content != null) {
                    holder.signature.setText(content);
                }
                holder.praisecount.setText(item.getTotal_praise() + "个赞");
                String url = item.getUserImage().getUrl();
                if (fragment == null) {
                    Glide.with(activity).load(url).placeholder(R.mipmap.place).thumbnail(0.3f).into(holder.pic);
                } else {
                    Glide.with(fragment).load(url).placeholder(R.mipmap.place).thumbnail(0.3f).into(holder.pic);
                }
                loadCircularImageView(holder.header_pic, item.getUserDetail().getAvatar_url());
                fillLin(item.getPraiseUser(), holder.praiseuser);

                List<CommentUser> comments = item.getCommentUser();
                if (comments != null) {
                    int v = Math.min(comments.size(), 2);
                    for (int i = 0; i < v; i++) {
                        CommentUser commentUser = comments.get(i);
                        String s = commentUser.getUser().getName() + " : " + commentUser.getContent();
                        holder.expanded.setText(s);
                    }
                    holder.commentCount.setText("全部" + comments.size() + "条评论");
                }

            } else if (o instanceof String) {
                holder.newcontent.setOnClickListener(this);
                holder.hotcontent.setOnClickListener(this);
                holder.hottopic.setOnClickListener(this);
                holder.hotuser.setOnClickListener(this);
            } else if (o instanceof Collection) {
                List<Banners> items = (List<Banners>) o;
                ActPagerAdapter adapter = new ActPagerAdapter(context, items, fragment);
                if (holder.pager.getAdapter() == null) {

                    holder.pager.setAdapter(adapter);
                    holder.pager.setCurrentItem(2 << 22);
                }

            } else if (o instanceof Sticker) {
                Sticker sticker = (Sticker) o;
                holder.sticker_content.setText(sticker.getContent());
                holder.sticker_count.setText(sticker.getCount() / 1000 + "k张图片");
                if (activity != null) {
                    Glide.with(activity).load(sticker.getCover_url())
                            .placeholder(R.mipmap.place)
                            .into(holder.sticker_pic);
                }
            } else if (o instanceof HotUser) {
                HotUser hotUser = (HotUser) o;
                for (int i = 0; i < holder.imageViews.size(); i++) {
                    Glide.with(activity).load(hotUser.getActivities().get(i).getUserImage().getUrl())
                            .placeholder(R.mipmap.place)
                            .into(holder.imageViews.get(i));
                }
                loadCircularImageView(holder.hot_header_pic, hotUser.getUser().getAvatar_url());
                holder.hot_header_name.setText(hotUser.getUser().getName());
                holder.hot_header_update.setText(hotUser.getUser().getDescription());
                holder.hot_header_more.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.mipmap.attention));
            }

        }
    }


    @Override
    public int getItemViewType(int position) {
        Object o = list.get(position);
        if (o instanceof UserActivity) {
            return 2;
        } else if (o instanceof List) {
            return 0;
        } else if (o instanceof String) {
            return 1;
        } else if (o instanceof Sticker) {
            return 3;
        } else if (o instanceof HotUser) {
            return 4;
        }
        return super.getItemViewType(position);
    }

    public void  clear(){
        notifyItemRangeRemoved(0,list.size());

        list.clear();

//        notifyItemRangeChanged(0,size);



    }
    public void add(int indext, Object o) {
        list.add(indext, o);
        notifyItemInserted(indext);
        notifyDataSetChanged();
    }

    public void addAll(Collection collection) {

        notifyItemRangeInserted(0, list.size()+collection.size());
        list.addAll(collection);

        notifyDataSetChanged();
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    public UserActivity getObjectItem(int position) {
        Object o = list.get(position);
        if (o instanceof UserActivity) {
            return ((UserActivity) o);
        }
        return null;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {

        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
    }

    public void fillLin(List<PraiseUser> praiseUsers, LinearLayout lin) {
        int dimension1 = (int) context.getResources().getDimension(R.dimen.mypadding);
        int dimension2 = (int) context.getResources().getDimension(R.dimen.fab_margin);
        lin.removeAllViews();
        int min = Math.min(praiseUsers.size(), 6);
        for (int i = 0; i < min; i++) {
            String url = praiseUsers.get(i).getAvatar_url();
            ImageView imageView = new ImageView(context);
            imageView.setAdjustViewBounds(true);
            loadCircularImageView(imageView, url);
            imageView.setPadding(dimension1, dimension2, dimension1, dimension2);
            lin.addView(imageView);
        }


    }

    public void loadCircularImageView(final ImageView imageView, String url) {

        if (fragment == null) {
            Glide.with(activity).load(url).asBitmap().override(90, 90).placeholder(R.mipmap.place)
                    .centerCrop().into(new BitmapImageViewTarget(imageView) {
                @Override
                protected void setResource(Bitmap resource) {
                    RoundedBitmapDrawable circularBitmapDrawable =
                            RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                    circularBitmapDrawable.setCircular(true);
                    imageView.setImageDrawable(circularBitmapDrawable);
                }
            });
        } else {
            Glide.with(fragment).load(url).asBitmap().override(90, 90).placeholder(R.mipmap.place)
                    .centerCrop().into(new BitmapImageViewTarget(imageView) {
                @Override
                protected void setResource(Bitmap resource) {
                    RoundedBitmapDrawable circularBitmapDrawable =
                            RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                    circularBitmapDrawable.setCircular(true);
                    imageView.setImageDrawable(circularBitmapDrawable);
                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(context, Social_Act_Activity.class);
        Intent intent1 = new Intent(context, SocialActTickerActivity.class);
        switch (v.getId()) {
            case R.id.social_recycler_item1_hotcontent:
                intent.putExtra("type", "hot");
                ArrayList list1 = new ArrayList<>();
                for (Object o : list) {
                    if (o instanceof UserActivity) {
                        list1.add(o);
                    }
                }
                String s = new Gson().toJson(list1);
                intent.putExtra("json", s);
                context.startActivity(intent);
                break;
            case R.id.social_recycler_item1_hottopic:
                intent1.putExtra("type", "hottopic");
                context.startActivity(intent1);
                break;
            case R.id.social_recycler_item1_hotuser:
                intent1.putExtra("type", "hotuser");
                context.startActivity(intent1);
                break;
            case R.id.social_recycler_item1_newcontent:
                intent.putExtra("type", "all");
                context.startActivity(intent);
                break;
            case R.id.social_recycler_item2_play:

                break;
        }

    }





    public static class SocialVH extends RecyclerView.ViewHolder implements View.OnClickListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener {

        private List<ImageView> imageViews= new ArrayList<>();

        private  RelativeLayout header;
        private  ImageView header_pic,pic;
        private  TextView header_update;
        private  TextView signature;
        private  TextView praisecount;
        private  LinearLayout praiseuser;
        private  TextView name;
        private final TextView expanded;
        private final TextView commentCount;
        private  ImageView header_more ;
        private final ImageView hotcontent;
        private final ImageView hottopic;
        private final ImageView hotuser;
        private final ImageView newcontent;
        private final ViewPager pager;
        private final TextView sticker_content;
        private final TextView sticker_count;
        private final ImageView sticker_pic;
        private final ImageView today_pic;
        private final ImageView yesterday_pic;
        private final ImageView longago_pic;
        private final RelativeLayout hot_header;
        private  TextView hot_header_name;
        private  ImageView hot_header_pic;
        private TextView hot_header_update;
        private  ImageView hot_header_more;
        private final ImageView play;
        private final ProgressBar progressBar;
        private final VideoView videoView;

        private boolean isFirst = true ;


        public SocialVH(View itemView) {
            super(itemView);
             header = (RelativeLayout) itemView.findViewById(R.id.social_recycler_item2_header);
             hot_header = (RelativeLayout) itemView.findViewById(R.id.social_hotuser_recycler_item_header);
             praiseuser = (LinearLayout) itemView.findViewById(R.id.social_recycler_item2_praiseuser);

            if (header != null) {
                name = ((TextView) header.findViewById(R.id.social_recycler_item2_header_name));
                header_pic = ((ImageView) header.findViewById(R.id.social_recycler_item2_header_pic));
                header_update = ((TextView) header.findViewById(R.id.social_recycler_item2_header_update));
                header_more = (ImageView) header.findViewById(R.id.social_recycler_item2_header_more);
            }
            if (hot_header != null) {
                hot_header_name = ((TextView) hot_header.findViewById(R.id.social_recycler_item2_header_name));
                hot_header_pic = ((ImageView) hot_header.findViewById(R.id.social_recycler_item2_header_pic));
                hot_header_update = ((TextView) hot_header.findViewById(R.id.social_recycler_item2_header_update));
                hot_header_more = (ImageView) hot_header.findViewById(R.id.social_recycler_item2_header_more);
            }

            signature = ((TextView) itemView.findViewById(R.id.social_recycler_item2_signature));
            expanded = (TextView) itemView.findViewById(R.id.social_recycler_item2_expanded);
             commentCount = (TextView) itemView.findViewById(R.id.social_recycler_item2_commentcount);
            praisecount = ((TextView) itemView.findViewById(R.id.social_recycler_item2_praisecount));

            pic = ((ImageView) itemView.findViewById(R.id.social_recycler_item2_pic));
            play = ((ImageView) itemView.findViewById(R.id.social_recycler_item2_play));

            progressBar = ((ProgressBar) itemView.findViewById(R.id.social_recycler_item2_progress));
            videoView = ((VideoView) itemView.findViewById(R.id.social_recycler_item2_videoView));


            pic = ((ImageView) itemView.findViewById(R.id.social_recycler_item2_pic));




            hotcontent = (ImageView) itemView.findViewById(R.id.social_recycler_item1_hotcontent);
            hottopic = (ImageView) itemView.findViewById(R.id.social_recycler_item1_hottopic);
            hotuser = (ImageView) itemView.findViewById(R.id.social_recycler_item1_hotuser);
            newcontent = (ImageView) itemView.findViewById(R.id.social_recycler_item1_newcontent);


            pager = (ViewPager) itemView.findViewById(R.id.social_recycler_item1_pager);


             sticker_content = (TextView) itemView.findViewById(R.id.social_sticker_recycler_item_content);
             sticker_count = (TextView) itemView.findViewById(R.id.social_sticker_recycler_item_count);
             sticker_pic = (ImageView) itemView.findViewById(R.id.social_sticker_recycler_item_pic);


            today_pic = (ImageView) itemView.findViewById(R.id.socia_hotuser_recycler_item_today);

            yesterday_pic = (ImageView) itemView.findViewById(R.id.socia_hotuser_recycler_item_yesterday);
            longago_pic = (ImageView) itemView.findViewById(R.id.socia_hotuser_recycler_item_longago);
            imageViews.add(today_pic);
            imageViews.add(yesterday_pic);
            imageViews.add(longago_pic);
            if(play!=null && videoView!=null){
                play.setOnClickListener(this);
                videoView.setOnPreparedListener(this);
                videoView.setOnErrorListener(this);
                videoView.setOnCompletionListener(this);
            }

        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.social_recycler_item2_play:
                    if(isFirst){
                        play.setVisibility(View.INVISIBLE);
                        pic.setVisibility(View.INVISIBLE);
                        videoView.setVisibility(View.VISIBLE);
                        videoView.start();
                    }else {
                        if (videoView.isPlaying()) {
                            videoView.pause();
                        } else {
                            videoView.resume();
                        }
                    }
                    isFirst = false;
                    break;
            }
        }

        @Override
        public void onPrepared(MediaPlayer mp) {
        }

        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            return false;
        }

        @Override
        public void onCompletion(MediaPlayer mp) {
            pic.setVisibility(View.VISIBLE);
            play.setVisibility(View.VISIBLE);
            videoView.setVisibility(View.GONE);
        }

    }





}
